//
//  TYZStarView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/8/29.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZStarView.h"


static const NSInteger kDefaultNumberOfStars = 5;
static const NSInteger kDefaultScorePercent = 1;
static NSString * const kStarImageStyleNormal = @"ic_my_star_all_nor";
static NSString * const kStarImageStyleHighlight = @"ic_my_star_all_sel";


@interface TYZStarView ()

@property (nonatomic, strong) UIView *upperView;
@property (nonatomic, strong) UIView *belowView;
@property (nonatomic, assign) NSInteger numberOfStars; ///< 星的个数
@property (nonatomic, assign) BOOL touchable; ///< 是否允许点击

@end

@implementation TYZStarView

- (instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStars touchable:(BOOL)touchable
{
    self.numberOfStars = (numberOfStars==0?kDefaultNumberOfStars:numberOfStars);
    self.touchable = touchable;
    _totalScore = 1.0;
    _currentScore = 0.0;
    return [self initWithFrame:frame];
}

- (void)initWithSubView
{
    _belowView = [self createStarViewWithImageName:kStarImageStyleNormal];
    _upperView = [self createStarViewWithImageName:kStarImageStyleHighlight];
    [self addSubview:_belowView];
    [self addSubview:_upperView];
    
    if (self.touchable)
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
    }
}

- (void)setCurrentScore:(CGFloat)currentScore
{
    if (_currentScore == currentScore)
    {
        return;
    }
    _currentScore = currentScore;
    [self setNeedsLayout];
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self];
    CGFloat offset = point.x;
    CGFloat offsetPercent = offset / self.bounds.size.width;
    if (self.isFullStarLimited)
    {
        offsetPercent = [self changeToCompleteStar:offsetPercent];
    }
    self.currentScore = offsetPercent * self.totalScore;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didTouchStarViewScore:index:)])
    {
        [self.delegate didTouchStarViewScore:self.currentScore index:self.tag];
    }
    if (self.touchStarViewScoreBlock)
    {
        self.touchStarViewScoreBlock(self.currentScore, self.tag);
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.currentScore > self.totalScore)
    {
        self.currentScore = self.totalScore;
    }
    else if (self.currentScore < 0)
    {
        _currentScore = 0;
    }
    else
    {
        _currentScore = self.currentScore;
    }
    
    CGFloat scorePercent = self.currentScore / self.totalScore;
    if (self.isFullStarLimited)
    {
        scorePercent = [self changeToCompleteStar:scorePercent];
    }
    self.upperView.frame = CGRectMake(0, 0, self.bounds.size.width * scorePercent, self.bounds.size.height);
}

- (UIView *)createStarViewWithImageName:(NSString *)imageName
{
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor = [UIColor clearColor];
    view.clipsToBounds = YES;
    
    for (NSInteger i = 0; i < _numberOfStars; i++)
    {
        UIImageView *starImageView = [[UIImageView alloc] init];
        starImageView.image = [UIImage imageNamed:imageName];
        starImageView.frame = CGRectMake(i * self.bounds.size.width / _numberOfStars,
                                         0,
                                         self.bounds.size.width / self.numberOfStars,
                                         self.bounds.size.height);
        starImageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:starImageView];
    }
    return view;
}

- (CGFloat)changeToCompleteStar:(CGFloat)percent
{
    if (percent <= 0.2)
    {
        percent = 0.2;
    }
    else if (percent > 0.2 && percent <= 0.4)
    {
        percent = 0.4;
    }
    else if (percent > 0.4 && percent <= 0.6)
    {
        percent = 0.6;
    }
    else if (percent > 0.6 && percent <= 0.8)
    {
        percent = 0.8;
    }
    else
    {
        percent = 1.0;
    }
    return percent;
}

@end

































